﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BLL;
using DAL;
using Model;
using IDAL;
using System.Data;


namespace Flowers.Controllers
{
    public class PurchaseController : Controller
    {
        //
        // GET: /Purchase/

        IFactory<IPurchaseGeneral, PurchaseDal> bll = new Factory<IPurchaseGeneral, PurchaseDal>();


        /// <summary>
        /// 进货的页面
        /// </summary>
        /// <returns></returns>
        public ActionResult Purchase_View()
        {
            List<PurchaseModel> list = (List<PurchaseModel>)Session["PurGenSession"];

            if(list!=null)
            {
                return View(list);
            }
            else
            {
                List<PurchaseModel> list2 = new List<PurchaseModel>();
                PurchaseModel mo = new PurchaseModel();
                mo.commodityID = "";
                mo.FlowersName = "";
                mo.PurchasePrice = 0;
                mo.PurchaseNumber = 0;
                list2.Add(mo);
                return View(list2);
            }
        }


        /// <summary>
        /// 商品一览的页面
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult PurchaseGeneral_View()
        {
            var select = Request["Select1"];

            List<PurchaseModel> list = bll.PurGenBll().iPurchaseGeneralList();

            if (select == "空")
            {
                list = bll.PurGenBll().iPurchaseGeneralList();
            }
            else if (select!=null)
            {
                list = list.Where(a => a.FlowersName.Contains(select)).ToList();
            }
            else
            {
                list = bll.PurGenBll().iPurchaseGeneralList();
            }
            
            return View(list);
        }

        /// <summary>
        /// 把商品一览的信息放入session  采购完成
        /// </summary>
        /// <param name="pur"></param>
        /// <returns></returns>
        public int PurchaseGeneralSession(PurchaseModel pur)
        {
            if (pur.PurchaseNumber==0)
            {
                return 1;
            }
            else
            {
                //更新一下session
                if(Session["PurGenSession"]!=null)
                {
                    List<PurchaseModel> list = (List<PurchaseModel>)Session["PurGenSession"];
                    list.Add(pur);
                    Session["PurGenSession"] = list;
                }
                else
                {
                    //给session赋值
                    List<PurchaseModel> list = new List<PurchaseModel>();
                    list.Add(pur);
                    Session["PurGenSession"] = list;
                }
                
                return 0;
            }
            
            
        }


        /// <summary>
        /// 进货（批量添加）
        /// </summary>
        /// <param name="order"></param>
        /// <returns></returns>
        public int PurchaseOrderAdd(Purchase_orderModel order)
        {
            List<PurchaseModel> list = (List<PurchaseModel>)Session["PurGenSession"];

            if (Session["PurGenSession"]!=null)
            {
                List<Purchase_orderModel> list2 = new List<Purchase_orderModel>();

                for (int i = 0; i < list.Count;i++ )
                {
                    Purchase_orderModel or = new Purchase_orderModel();
                    or.commodityID = list[i].commodityID;
                    or.FlowersName = list[i].FlowersName;
                    or.PurchasePrice = list[i].PurchasePrice;
                    or.PurchaseNumber = list[i].PurchaseNumber;
                    or.Invoice_number = order.Invoice_number;
                    or.Supplier = order.Supplier;
                    list2.Add(or);
                }

                if(list2.Count>0)
                {
                    return  bll.PurGenBll().iPurchaseOrderAddAll(list2);
                }
                else
                {
                    return 0;
                }
                


            }
            else
            {
                return 0;
            }

        }


    }
}
